import java.util.Arrays;

public class Nice {
//数组二分查找
    public static int binarySearch(int[] arr,int key) {
        int i = 0;
        int j = arr.length - 1;
        while (i <= j) {
            int middle = (i + j) / 2;
            if (arr[middle] == key) {
                return middle;
            } else if (key < arr[middle]) {
                j = middle - 1;
            } else {
                i = middle + 1;
            }
        }
        return -1;
    }
    //数组逆置
    public static void nizhi(int[] array) {
        int i = 0;
        int j = array.length-1;
        while (i < j) {
            int tmp = array[i];
            array[i] = array[j];
            array[j] = tmp;
            i++;
            j--;
        }

    }

//数组冒泡排序
    public static void bubbleSort(int[] arra) {
        for (int i = 0; i < arra.length - 1; i++) {
            boolean flg = false;

            for (int j = 0; j < arra.length - 1 - i; j++) {
                if (arra[j] > arra[j + 1]) {
                    int tmp = arra[j];
                    arra[j] = arra[j + 1];
                    arra[j + 1] = tmp;
                    flg = true;
                }
            }
            if (flg == false) {
                return ;
            }

        }

    }

    public static void main(String[] args){
            int[] array = {1, 4, 33, 22, 34, 12, 233, 34};
            //Arrays.sort(array);
            // System.out.println(Arrays.toString(array));
            //System.out.println(binarySearch(array, 22));
            // System.out.println(Arrays.binarySearch(array, 22));
            // nizhi(array);
            //System.out.println(Arrays.toString(array));
            bubbleSort(array);
        System.out.println(Arrays.toString(array));

    }
    //数组转换字符串打印
    public static String  myToString(int[] array) {
        if (array == null) {
            return "null";
        }
        String ret = "[";
        for (int i = 0; i < array.length; i++) {
            ret += array[i];
           if (i != array.length-1) {
               ret += ",";
           }
        }
        ret += "]";
        return ret;
    }
    public static void main2(String[] args) {
        int[] array2 = {1,2,3,4,5};
        int[] ret = Arrays.copyOf(array2,array2.length);
        //System.out.println(Arrays.toString(ret));
        int[] ret1 = Arrays.copyOfRange(array2,1,3);
        System.out.println(Arrays.toString(ret1));


        int[] array = {1,2,3,4,45,56,66,99};
        //System.out.println(Arrays.toString(array));

        //String ret = myToString(array);

       // System.out.println(myToString(array));


        //数组拷贝
        int[] array1 = {1,2,3,4,5,44,33,66};
        int[] copy = new int[array1.length];
        for (int i = 0; i < array1.length; i++) {
            copy[i] = array1[i];
        }
       // System.out.println(Arrays.toString(copy));


    }
    public static void main1(String[] args) {

        int[] array = {1,2,3,4,5,6,7};
        int[] array1 = new int[]{1,2,3,5,6,9};
        int[] array2 = new int[6];
    }
}
